"ui";
ui.layout(
    <vertical id="parent">
        <horizontal>
            <input layout_weight="1"  id="address"/>
            <button id="search" text="搜索" />
        </horizontal>
        <text>搜索结果</text>
        <list id="列表" h="200">
            <vertical padding="16">
                <text id="名字" text="{{name}}" textSize="15" />
                <horizontal>
                    <text text="{{cityname}} " textSize="12" />
                    <text text="{{adname}} " textSize="12" />
                    <text text="{{address}}" textSize="12" />
                </horizontal>
            </vertical>1
        </list>
        <text>选中地址经纬度</text>
        <text id="经纬度"></text>
    </vertical>
);

// 监听点击事件
var list = []
ui.列表.on("item_click", function (item, i, itemView, listView) {
    var data = list[i];
    ui.address.text(itemView.名字.text());
    ui.经纬度.text(data.location);
});

ui.search.on("click", function () {
    getAddress(ui.address.text(), ui);
});
var key = '7f03f48f47eb5767c3cb692156ba4909'
var url = 'https://restapi.amap.com/v3'
function setDataSource() {
}
function getAddress(keywords) {
    // 周边搜索
    let path = url + '/place/text?key=' + key + '&keywords=' + keywords
    http.get(path, {
        keywords: keywords,
    }, function (res, err) {
        if (err) {
            console.error(err);
            log(10);
            return;
        }
        try {
            // 解析数据
            list = res.body.json().pois;
            if (list.length == 0) {
                log('没有搜索到结果');
            } else {
                // 赋值
                ui.run(() => {
                    ui.列表.setDataSource(list);
                })
                log(1);
            }

        } catch (e) {
            console.error(e);
        }
    });
}
